package com.ksd.web.controller.system;

import com.ksd.api.beans.SongData;
import com.ksd.common.core.controller.BaseController;
import com.ksd.common.core.domain.AjaxResult;
import com.ksd.framework.web.domain.Server;
import com.ksd.framework.web.domain.server.*;
import com.ksd.quartz.util.TaskConstans;
import com.ksd.system.domain.*;
import com.ksd.system.service.*;
import com.ksd.web.controller.domain.ContentData;
import com.ksd.web.controller.domain.FirstDataResult;
import com.ksd.web.controller.domain.OnlineDevice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

@RestController
public class FirstController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(FirstController.class);
    @Autowired
    private ISysTvDataService sysTvDataService;
    @Autowired
    private ISysTvTypeService sysTvTypeService;
    @Autowired
    private ISysChannelTwiceService sysChannelTwiceService;
    @Autowired
    private ISysChannelOneService sysChannelOneService;
    @Autowired
    private ISysChannelService sysChannelService;
    @Autowired
    private ISysChannelTemplateService sysChannelTemplateService;
    @Autowired
    private ISysFreqService sysFreqService;
    @Autowired
    private ISysMovieService sysMovieService;
    @Autowired
    private ISysMusicDataService sysMusicDataService;

    @GetMapping("/usb/contentFileData")
    public AjaxResult getContentFileData() {

        FirstDataResult firstDataResult = new FirstDataResult();

        List<ContentData> contentDataList = new ArrayList<>();
        SysTvType sysTvTypeMovie = sysTvTypeService.selectSysTvTypeById(Long.valueOf(SysTvType.movieDefaultId));
            SysTvData sysTvData = new SysTvData();
            sysTvData.setTypeId(sysTvTypeMovie.getId());
            List<SysTvData> sysTvDataList = sysTvDataService.selectSysTvDataList(sysTvData);

            for (SysTvData tvData : sysTvDataList) {
                Long id = tvData.getId();
                SysMovie sysMovie = new SysMovie();
                sysMovie.setTypeData(id);
                List<SysMovie> movieList = sysMovieService.selectSysMovieList(sysMovie);
                if (movieList.size() != 0){
                    ContentData contentData = new ContentData();
                    contentData.setCat_id(id.toString());
                    contentData.setCat_name(tvData.getName());
                    contentData.setNum(String.valueOf(movieList.size()));
                    contentDataList.add(contentData);
                }
            }


        firstDataResult.setAccess(contentDataList);

        List<ContentData> songDataList = new ArrayList<>();
        SysTvType sysTvTypeSong = sysTvTypeService.selectSysTvTypeById(Long.valueOf(SysTvType.songDefaultId));
        sysTvData.setTypeId(sysTvTypeSong.getId());
        List<SysTvData> sysTvDataSongList = sysTvDataService.selectSysTvDataList(sysTvData);

        for (SysTvData tvData : sysTvDataSongList) {
            Long id = tvData.getId();
            SysMusicData sysMusicData = new SysMusicData();
            sysMusicData.setTypeData(id);
            List<SysMusicData> sysMusicDataList = sysMusicDataService.selectSysMusicDataList(sysMusicData);
            if (sysMusicDataList.size() != 0){
                ContentData songData = new ContentData();
                songData.setCat_id(id.toString());
                songData.setCat_name(tvData.getName());
                songData.setNum(String.valueOf(sysMusicDataList.size()));
                songDataList.add(songData);
            }
        }
        firstDataResult.setAccessSong(songDataList);

        Server server = new Server();
        try {
            server.copyTo();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
                
        Cpu cpu = server.getCpu();
        Mem mem = server.getMem();
        Jvm jvm = server.getJvm();

        DecimalFormat df = new DecimalFormat("#.00");
        String formattedNumber = df.format(cpu.getSys());
        String formattedNumber1 = df.format(mem.getUsed());
        String formattedNumber2 = df.format(server.getSysFiles().get(0).getUsage());

        firstDataResult.setCpu(formattedNumber+"%");
        firstDataResult.setMemory(formattedNumber1+"%");
        firstDataResult.setDisk(formattedNumber2+"%");


//        List<SysChannelOne> sysChannelOnesSelected = sysChannelOneService.selectSysChannelListByOrStatus(TaskConstans.status_selected,TaskConstans.status_selected);
//        List<SysChannelTwice> sysChannelTwicesSelected = sysChannelTwiceService.selectSysChannelListByOrStatus(TaskConstans.status_selected, TaskConstans.status_selected);

        SysChannelTemplate selectChannel = new SysChannelTemplate();
        selectChannel.setStatus(TaskConstans.status_selected);
        selectChannel.setBoardId(Long.valueOf(TaskConstans.boardValue1));
        List<SysChannelTemplate> sysChannelTemplates1 = sysChannelTemplateService.selectSysChannelTemplateList(selectChannel);
        selectChannel.setBoardId(Long.valueOf(TaskConstans.boardValue2));
        List<SysChannelTemplate> sysChannelTemplates2 = sysChannelTemplateService.selectSysChannelTemplateList(selectChannel);

        SysChannelTemplate temp = new SysChannelTemplate();
        temp.setStatus(TaskConstans.status_selected);
        temp.setBoardId(Long.valueOf(TaskConstans.boardValueCustom));
        List<SysChannelTemplate> tempList = sysChannelTemplateService.selectSysChannelTemplateList(temp);

        List<SysChannelOne> sysChannelOnesInit = sysChannelOneService.selectSysChannelListByAndStatus(TaskConstans.status_init);
        List<SysChannelTwice> sysChannelTwicesInit = sysChannelTwiceService.selectSysChannelListByAndStatus(TaskConstans.status_init);

//        System.out.println(sysChannelOnesInit.size());
//        System.out.println(sysChannelTwicesInit.size());

//        List<SysChannel> sysChannelOneInit = sysChannelService.selectSysChannelListByAndStatus(TaskConstans.status_init, SysChannel.table_one);
//        List<SysChannel> sysChannelTwiceInit = sysChannelService.selectSysChannelListByAndStatus(TaskConstans.status_init, SysChannel.table_twice);
//        System.out.println(sysChannelOneInit.size());
//        System.out.println(sysChannelTwiceInit.size());
        SysChannel sysChannel = new SysChannel();
        sysChannel.setBoardId(1L);
//        List<SysChannel> sysChannels = sysChannelService.selectSysChannelList(sysChannel, SysChannel.table_one);
//        System.out.println("sysChannel one: "+sysChannels.size());
//        SysChannel sysChannel1 = sysChannelService.selectSysChannelById(1L, SysChannel.table_twice);
//        System.out.println("=========id==="+sysChannel1.getName());
//        SysChannel sysChannel2 = sysChannelService.selectSysChannelByName("CCTV-1 标清", SysChannel.table_twice);
//        System.out.println("========name===="+sysChannel2.getName());

        SysFreq sysFreq = sysFreqService.selectSysFreqById(1L, SysFreq.table_one);
//        System.out.println("freq:"+sysFreq.getFreq());
//        List<SysFreq> sysFreqs = sysFreqService.selectSysFreqList(sysFreq, SysFreq.table_one);
//        List<SysFreq> sysFreqs1 = sysFreqService.selectSysFreqListByFreq(sysFreq, SysFreq.table_one);
//        System.out.println("sysFreqs1:"+sysFreqs1.size()+"============"+sysFreq.getFreq()+"======");
        SysFreq sysFreqNew = new SysFreq();
        sysFreqNew.setFreq(sysFreq.getFreq());
//        List<SysFreq> sysFreqs2 = sysFreqService.selectSysFreqListByFreq(sysFreqNew, SysFreq.table_twice);
//        System.out.println("sysFreqs2:"+sysFreqs2.size());

        OnlineDevice onlineDevice = new OnlineDevice();
        onlineDevice.setOnline(sysChannelTemplates1.size() + sysChannelTemplates2.size());
        onlineDevice.setCustomline(tempList.size());
        onlineDevice.setOutline(sysChannelOnesInit.size()+sysChannelTwicesInit.size());
        firstDataResult.setOnlineDevice(onlineDevice);
        String runTime = jvm.getRunTime();
        firstDataResult.setUpTime(runTime);

        firstDataResult.setPhpStatus(false);
        firstDataResult.setPushCardStatus(true);
        firstDataResult.setLiveCardStatus(true);
        firstDataResult.setRouterStatus(true);
        firstDataResult.setX86(true);

        return success(firstDataResult);
    }

    public static void main(String[] args) {
        double number = 123.45678;
        // 创建 DecimalFormat 对象，指定保留两位小数的模式
        DecimalFormat df = new DecimalFormat("#.00");
        String formattedNumber = df.format(number);
        System.out.println("使用 DecimalFormat 保留两位小数: " + formattedNumber);
    }
}
